package views;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import java.util.ArrayList;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;

import model.DbHelper;
import presenters.RentACarPresenter;


public class RentACarPanel extends JPanel {
	private JTextField textField;
	private JTextField textField_1;
	JTable table;
	JButton rentCarBtn = new JButton("Rent Car");
	RentACarPresenter myPresenter;
	Object[][] addInData;
	String[] columnNames = {"Car Model",
            "Car Type",
            "Location",
            "Color",
            "Hourly Rate",
            "Discounted Rate",
            "Discounted Rate (Daily Driver)",
            "Daily Rate",
            "Seating Capacity",
            "Transmission Type",
            "Bluetooth Connectivity",
            "Auxilary Cable",
            "Available Until",
            "Estimated Cost",
	};

	/**
	 * Create the panel.
	 */
	public RentACarPanel() {
		setLayout(new BorderLayout());
		
		Box verticalBox = Box.createHorizontalBox();
		DbHelper myHelper = new DbHelper(); // used to get data for ComboBoxes
		
		JLabel lblPickupTime = new JLabel("Pick-Up Time[\"month\", \"day\", \"year\", \"hour\", \"minute\"]:");
		verticalBox.add(lblPickupTime);
		
		textField = new JTextField();
		verticalBox.add(textField);
		textField.setColumns(10);
		
		JLabel lblReturnTime = new JLabel("Return Time[\"month\", \"day\", \"year\", \"hour\", \"minute\"]:");
		verticalBox.add(lblReturnTime);
		
		textField_1 = new JTextField();
		verticalBox.add(textField_1);
		textField_1.setColumns(10);
		
		JLabel lblLocation = new JLabel("Location:");
		verticalBox.add(lblLocation);
		
		JComboBox comboBox = new JComboBox(myHelper.getLocations());
		verticalBox.add(comboBox);
		
		JLabel lblCars = new JLabel("Cars (by type):");
		verticalBox.add(lblCars);
		
		JComboBox comboBox_1 = new JComboBox(myHelper.getCarTypes());
		verticalBox.add(comboBox_1);
		
		JButton btnSearch = new JButton("SEARCH");
		verticalBox.add(btnSearch);
		btnSearch.addActionListener(new MyActionListener());
		
		Component verticalStrut = Box.createVerticalStrut(10);
		verticalBox.add(verticalStrut);
		
	//	JLabel lblRentACar = new JLabel("Rent A Car");
	//	add(lblRentACar);
		
		Object[][] data = {
				{"1","1","1","1","1","2","1","1","2","1","2","1","1","2"}
			};
		
		table = new JTable(data, columnNames);

		myPresenter = new RentACarPresenter(this);
		
		add(new JScrollPane(table), BorderLayout.NORTH);
		rentCarBtn.addActionListener(new MyActionListener2());
		add(rentCarBtn, BorderLayout.WEST);
		add(verticalBox, BorderLayout.EAST);

	} // close constructor
	
	public void setTable(ResultSet rs) {
		String sql = "";
		/*
		ArrayList<Object> newData = new ArrayList<Object>();
		rs = executeSQLQuery(sql);
		try {
			rs.first();
			String carModel = rs.getString("CAR_MODEL");
			String carType = rs.getString("CAR_TYPE");
			String carLoc = rs.getString("LOCATION");
			String carColor = rs.getString("COLOR");
			String carHourRateOcc = rs.getString("HOUR_RATE_OCC");
			String carDiscRateFreq = rs.getString("DISC_RATE_FREQ");
			String carDiscRateDaily = rs.getString("DISC_RATE_DAILY");
			String carDailyRate = rs.getString("DAILY RATE");
			String carSeatingCap = rs.getString("SEATING_CAP");
			String carTranType = rs.getString("TRAN_TYPE");
			String carBluetooth = rs.getString("BLUETOOTH");
			String carAux = rs.getString("AUX");
			String carAvailable = rs.getString("AVAILABLE");
			String carEstCost = rs.getString("EST_COST");
			
			//RUN THIS LOOP FOR AS MANY TIMES AS WE NEED DATA
			// while(rs.next()) {
			String carModel = rs.getString("CAR_MODEL");
			String carType = rs.getString("CAR_TYPE");
			String carLoc = rs.getString("LOCATION");
			String carColor = rs.getString("COLOR");
			String carHourRateOcc = rs.getString("HOUR_RATE_OCC");
			String carDiscRateFreq = rs.getString("DISC_RATE_FREQ");
			String carDiscRateDaily = rs.getString("DISC_RATE_DAILY");
			String carDailyRate = rs.getString("DAILY RATE");
			String carSeatingCap = rs.getString("SEATING_CAP");
			String carTranType = rs.getString("TRAN_TYPE");
			String carBluetooth = rs.getString("BLUETOOTH");
			String carAux = rs.getString("AUX");
			String carAvailable = rs.getString("AVAILABLE");
			String carEstCost = rs.getString("EST_COST");
				newData.add(carModel);
				newData.add(carType);
				newData.add(carLoc);
				newData.add(carColor);
				newData.add(carHourRateOcc);
				newData.add(carDiscRateFreq);
				newData.add(carDiscRateDaily);
				newData.add(carDailyRate);
				newData.add(carSeatingCap);
				newData.add(carTranType);
				newData.add(carBluetooth);
				newData.add(carAux);
				newData.add(carAvailable);
				newData.add(carEstCost);
			}
		}
		finally {
		}
		addInData = new Object[newData.size()/14][13];
		int k = 0;
		for (int i = 0; i < newData.size()/14; i++)
		{
			for (int j = 0; j < 14; j++)
			{
				addInData[i][j] = newData.get(k);
				k++;
			}
		}
		table = new JTable(addInData, columnNames);
		revalidate();
		*/
	}
	
	private class MyActionListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent arg0) {
			// TODO Auto-generated method stub
			String pickup = textField.getText();
			String dropoff = textField_1.getText();
			String location = null; // TODO
			
		}
		
	}
	
	private class MyActionListener2 implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent arg0) {
			System.out.println(table.getSelectedRow());
		}

	}

}
